package com.nulldev.util.io.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.nulldev.util.data.Variables;
import com.nulldev.util.io.IOUtils;

public class TransferToBackport {

	private static final int DEFAULT_BUFFER_SIZE = IOUtils.MEMORY_ALLOC_BUFFER;

	public static long transferTo(final InputStream is, final OutputStream out) throws IOException {
		Variables.requireNonNull(out, "out");
		long transferred = 0;
		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		int read;
		while ((read = is.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
			out.write(buffer, 0, read);
			transferred += read;
		}
		return transferred;
	}
}
